package com.maizhe.service.impl;

import com.github.pagehelper.PageHelper;
import com.maizhe.dao.OrganizationMapper;
import com.maizhe.dao.UserMapper;
import com.maizhe.dao.UserRoleMapper;
import com.maizhe.entity.Organization;
import com.maizhe.entity.Pwd;
import com.maizhe.entity.User;
import com.maizhe.entity.UserRole;
import com.maizhe.entity.vo.UserVo;
import com.maizhe.service.UserService;
import com.maizhe.util.JsonResult;
import com.maizhe.util.UUIDGenerator;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserRoleMapper userRoleMapper;
	@Autowired
	private OrganizationMapper organizationMapper;

	public JsonResult<User> login(User user) {
		JsonResult<User> result=new JsonResult<User>();
		User u=userMapper.findUser(user);
		if (u==null){
			result.setState(1);
			result.setMessage("用户名或密码错误");
		}else {
			result.setState(0);
			result.setData(u);
		}
		return result;
	}

	public List<UserVo> findAll(User user) {
		PageHelper.startPage(user.getPage(),user.getRows());
		List<User> list=userMapper.findAll(user);
		List<UserVo> userVoList=new ArrayList<UserVo>();
		for(User u:list){
			UserVo userVo = getUserVo(u);
			userVoList.add(userVo);
		}
		return userVoList;
	}

	private UserVo getUserVo(User u) {
		UserVo userVo=new UserVo(u);
		List<Map<String,Object>> userRoles=userRoleMapper.findById(u.getId());//一个用户可以多个角色
		userVo.setRoles(parseRoleNames(userRoles));
		userVo.setRoleIds(parseRoleIds(userRoles));
		Organization org=organizationMapper.findById(u.getOrganizationId());//一个用户只能一个部门
		if(org!=null){
            userVo.setOrganizationName(org.getName());
        }
		return userVo;
	}


	public Integer countAll(User user) {
		//PageHelper.startPage(user.getPage(),user.getRows());
		return userMapper.countAll(user);
	}

	public int saveUser(UserVo user) {
		if (user.getId()==null || "".equalsIgnoreCase(user.getId())) {//插入
			String userId = UUIDGenerator.getUUID();
			//保存user信息
			User u = new User(user);
			u.setId(userId);
			u.setSalt(user.getPassword());
			u.setCreateTime(new Date());
			int w = userMapper.saveUser(u);
			//保存user-role
            user.setId(u.getId());
			saveUserRole(user);
			return w;
		}else{//编辑
			User u = new User(user);
			u.setId(user.getId());
			u.setCreateTime(new Date());
			int k=userMapper.editUser(u);

			//user_role
			//先删除后添加
			userRoleMapper.deleteByUserId(user.getId());
			saveUserRole(user);
			return k;
		}
	}

	private void saveUserRole(UserVo user) {
		String[] roles = user.getRoleIds().split(",");
		if (roles.length > 0) {
            for (int i = 0; i < roles.length; i++) {
                String id= UUIDGenerator.getUUID();
                int n = userRoleMapper.saveUserRole(new UserRole(id,user.getId(), roles[i]));
            }
        }
	}

	public int delete(String id) {
		return userMapper.remove(id);
	}

	public UserVo findById(String id) {
		User user=userMapper.findById(id);
		return getUserVo(user);
	}

	public UserVo findByLoginName(String loginName) {
		User user=userMapper.findUserByName(loginName);
		Assert.notNull(user,"登录名"+loginName+"不存在!");
		return getUserVo(user);
	}

	public List<Map<String, Object>> findSalesAll() {
		List<Map<String, Object>> maps=userMapper.findSalesAll();
		for(Map<String, Object> map:maps){
			map.put("checked",true);
		}
		return  maps;
	}

	public List<Map<String, Object>> findAllManager() {
		List<Map<String, Object>> maps=userMapper.findAllManager();
		return  maps;
	}

	public JsonResult<Integer> setPwd(Pwd pwd) {
		JsonResult<Integer> result=new JsonResult<Integer>();
		User user=userMapper.findById(pwd.getId());
		if(!pwd.getOldPwd().equalsIgnoreCase(user.getPassword())){
			result.setState(1);
			result.setMessage("密码输入有误");
		}else {
			User user1=new User();
			user1.setId(pwd.getId());
			user1.setPassword(pwd.getNewPwd());
			int i=userMapper.editUser(user1);
			result.setData(i);
			result.setState(0);
		}
		return result;
	}


	private String parseRoleNames(List<Map<String, Object>> userRoles) {
		String roleStr="";
		if(userRoles.size()>0){
			for(Map<String,Object> role:userRoles){
				String name=(String) role.get("name");
				roleStr=roleStr+name+",";
			}
			roleStr=roleStr.substring(0,roleStr.length()-1);
		}
		return roleStr;
	}
	private String parseRoleIds(List<Map<String, Object>> userRoles) {
		String idStr="";
		if(userRoles.size()>0){
			for(Map<String,Object> role:userRoles){
				String id=(String) role.get("roleId");
				idStr=idStr+id+",";
			}
			idStr=idStr.substring(0,idStr.length()-1);
		}
		return idStr;
	}
}
